package io.gitee.kotle.android.utils

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityOptionsCompat
import androidx.fragment.app.Fragment
import io.gitee.kotle.android.mvvm.binding.FragmentContainerActivity

/**
 *
 * 描述：
 *
 * 创建人：jiale.wei
 * 创建时间：2021/11/22 10:04 上午
 *
 */

/**
 * 利用[FragmentContainerActivity]容器启动一个[Fragment]
 */
inline fun <reified FG : Fragment> Context?.startFragment(
    arguments: Bundle? = null,
    optionsCompat: ActivityOptionsCompat? = null,
    intentBlock: (Intent) -> Unit = {
        if (this != null) {
            ActivityCompat.startActivity(
                this,
                it,
                optionsCompat?.toBundle()
            )
        }
    },
) {
    FragmentContainerActivity.invoke<FG>(this, arguments, optionsCompat, intentBlock)
}

/**
 * 利用[FragmentContainerActivity]容器启动一个[Fragment]
 */
inline fun <reified FG : Fragment> Fragment?.startFragment(
    arguments: Bundle? = null,
    optionsCompat: ActivityOptionsCompat? = null,
    intentBlock: (Intent) -> Unit = {
        if (this != null) {
            ActivityCompat.startActivity(
                this.requireContext(),
                it,
                optionsCompat?.toBundle()
            )
        }
    },
) {
    FragmentContainerActivity.invoke<FG>(this, arguments, optionsCompat, intentBlock)
}

/**
 * 利用[FragmentContainerActivity]容器启动一个[Fragment]
 */
inline fun <reified FG : Fragment> View?.startFragment(
    arguments: Bundle? = null,
    optionsCompat: ActivityOptionsCompat? = null,
    intentBlock: (Intent) -> Unit = {
        val context = this?.context
        if (context != null) {
            ActivityCompat.startActivity(
                context,
                it,
                optionsCompat?.toBundle()
            )
        }
    },
) {
    val context = this?.context ?: return
    FragmentContainerActivity.invoke<FG>(context, arguments, optionsCompat, intentBlock)
}

/**
 * 启动activity
 */
inline fun <reified A : Activity> Context?.startActivity(
    optionsCompat: ActivityOptionsCompat? = null,
    intentBlock: (Intent) -> Unit = {
        if (this != null) {
            ActivityCompat.startActivity(
                this,
                it,
                optionsCompat?.toBundle()
            )
        }
    },
) {
    this ?: return
    intentBlock.invoke(Intent(this, A::class.java))
}

/**
 * 启动activity
 */
inline fun <reified A : Activity> Fragment?.startActivity(
    optionsCompat: ActivityOptionsCompat? = null,
    intentBlock: (Intent) -> Unit = {
        val context = this?.context
        if (context != null) {
            ActivityCompat.startActivity(
                context,
                it,
                optionsCompat?.toBundle()
            )
        }
    },
) {
    val context = this?.context ?: return
    intentBlock.invoke(Intent(context, A::class.java))
}

/**
 * 启动activity
 */
inline fun <reified A : Activity> View?.startActivity(
    optionsCompat: ActivityOptionsCompat? = null,
    intentBlock: (Intent) -> Unit = {
        val context = this?.context
        if (context != null) {
            ActivityCompat.startActivity(
                context,
                it,
                optionsCompat?.toBundle()
            )
        }
    },
) {
    val context = this?.context ?: return
    intentBlock.invoke(Intent(context, A::class.java))
}